package com.glq1218.report.controller;

import com.glq1218.report.bo.ReportBO;
import com.glq1218.report.bo.QueryReportBO;
import com.glq1218.report.dto.ReportDTO;
import com.glq1218.report.dto.QueryReportDTO;
import com.glq1218.domain.*;
import com.glq1218.report.bo.AddReportBO;
import com.glq1218.report.service.ReportService;
import com.glq1218.report.dto.AddReportDTO;
import com.glq1218.util.BeanCopyUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author glq1218
 * @description 举报控制器
 * @since 2023/4/21 15:00
 */
@RestController
@RequestMapping("/api/report")
@RequiredArgsConstructor
public class ReportController {
    private final ReportService reportService;

    @GetMapping
    public R<PaginationResult<ReportDTO>> queryReportList(PaginationParam paginationParam, SortParam sortParam, IntervalStr intervalStr, QueryReportDTO queryReportDTO) {
        QueryReportBO queryReportBO = BeanCopyUtils.copyBean(queryReportDTO, QueryReportBO.class);
        PaginationResult<ReportBO> paginationResult = reportService.queryReportList(paginationParam, sortParam, intervalStr, queryReportBO);
        // 获取BO转换为DTO
        List<ReportDTO> reportDTOList = new ArrayList<>();
        // 结果不为空才能进行转换
        if (Objects.nonNull(paginationResult.getRecords())) {
            List<ReportBO> records = paginationResult.getRecords();
            reportDTOList = BeanCopyUtils.copyBeanList(records, ReportDTO.class);
        }
        return R.ok(PaginationResult
                .<ReportDTO>builder()
                .total(paginationResult.getTotal())
                .records(reportDTOList)
                .build()
        );
    }

    @PostMapping
    public R<?> addReport(@RequestBody AddReportDTO addReportDTO) {
        AddReportBO addReportBO = BeanCopyUtils.copyBean(addReportDTO, AddReportBO.class);
        return R.ok(reportService.addReport(addReportBO));
    }
}
